import storage from '@system.storage'

export const getStore = (key) => {
  return new Promise((resolve) => {
    storage.get({
      key: key,
      success: function(data) {
        try {
          const d = JSON.parse(data)
          resolve(d)
        } catch (err) {
          console.error(err)
          resolve()
        }
      },
      fail: function(err) {
        console.error(err)
        resolve()
      }
    })
  })
}

export const setStore = (key, val) => {
  return new Promise((resolve) => {
    try {
      const value = JSON.stringify(val)
      storage.set({
        key: key,
        value: value,
        success: function(data) {
          resolve(true)
        },
        fail: function(data, code) {
          resolve(false)
        }
      })
    } catch (err) {
      resolve(false)
    }
    
  })
}